<?php

namespace app\common\model\delivery;

use think\Model;

/**
 * 长字符串模型
 */
class Text Extends Model
{
    public static function add($data,$table='')
    {
        if(is_array($data)){
            if(count($data)==0){
                return;
            }
            $data=json_encode($data,JSON_UNESCAPED_UNICODE);
        }else{
            if($data==''){
                return;
            }
        }
        $text=new self();
        $text->content=$data;
        $text->tablename=$table;
        $text->save();
        return $text->id;
    }

    public static function get($id, $with = [], $cache = false)
    {
        $data=self::find($id);
        if($with===true){
            return $data->content;
        }
        if(str_starts_with($data->content,'[') && str_ends_with($data->content,']')){
            return json_decode($data->content,true);
        }
        if(str_starts_with($data->content,'{') && str_ends_with($data->content,'}')){
            return json_decode($data->content,true);
        }
        return $data->content;
    }
}
